package cc.lishixi.tool;

import org.apache.pivot.collections.immutable.ImmutableList;
import org.apache.pivot.wtk.*;

import java.io.File;

public class MainWindow extends Window {
    private TextInput txtDatPath;
    private FileBrowserSheet fileBrowserSheet;
    private MainWindow window;
    private File file;

    MainWindow() {
        window = this;
        Label lblDat = new Label("Dat文件");
        txtDatPath = new TextInput();
        txtDatPath.setPrompt(".dat文件");
        Button btnOpen = new PushButton("打开");
        fileBrowserSheet = new FileBrowserSheet(FileBrowserSheet.Mode.OPEN);

        btnOpen.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button button) {
                fileBrowserSheet.open(window, new SheetCloseListener() {
                    @Override
                    public void sheetClosed(Sheet sheet) {
                        if (sheet.getResult()) {
                            ImmutableList<File> selectedFiles = fileBrowserSheet.getSelectedFiles();
                            file = selectedFiles.get(0);
                            txtDatPath.setText(file.getAbsolutePath());
                        }
                    }
                });
            }
        });
        StyleDictionary styles;
        Border border = new Border();
        styles = border.getStyles();
        styles.put("padding", 6);

        BoxPane boxPane = new BoxPane(Orientation.HORIZONTAL);
        styles = boxPane.getStyles();
        styles.put("horizontalAlignment", HorizontalAlignment.CENTER);
        border.setContent(boxPane);

        boxPane.add(lblDat);
        boxPane.add(txtDatPath);
        boxPane.add(btnOpen);

        setContent(border);
    }


    @Override
    public MainWindow getWindow() {
        return window;
    }

}
